package com.hzz.service.dsync;

import com.alibaba.fastjson.JSONObject;
import com.hzz.api.eybase.*;
import com.hzz.dto.dsync.EyBasePageResultDto;
import com.hzz.dto.eybase.*;
import com.hzz.framework.Response;
import com.hzz.framework.util.CommonConstant;
import com.hzz.task.EyBasePageDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class EyBaseTaskDSync extends BaseDSync {
    @Autowired
    IEyBaseRetrieveGmService eyBaseRetrieveGmService;
    @Autowired
    IEyBaseRetrieveHydlService eyBaseRetrieveHydlService;
    @Autowired
    IEyBaseRetrieveRegionService eyBaseRetrieveRegionService;
    @Autowired
    IEyBaseRetrieveChnService eyBaseRetrieveChnService;
    @Autowired
    IEyBaseRetrieveChnSliceService eyBaseRetrieveChnSliceService;
    @Autowired
    IEyBaseRetrieveGmHydlService eyBaseRetrieveGmHydlService;
    @Autowired
    IEyBaseRetrieveChnGmService eyBaseRetrieveChnGmService;
    @Autowired
    IEyBaseRetrieveReservoirsService eyBaseRetrieveReservoirsService;
    @Autowired
    IEyBaseRetrieveReservoirsSliceService eyBaseRetrieveReservoirsSliceService;
    @Autowired
    IEyBaseRetrieveReservoirsGmService eyBaseRetrieveReservoirsGmService;

    @Value("${dsync.eyBase.retrieve-rivers}")
    String retrieveRiversApi;
    @Value("${dsync.eyBase.retrieve-reach}")
    String retrieveReachPageApi;
    @Value("${dsync.eyBase.retrieve-area}")
    String eyBaseRetrieveRegionApi;
    @Value("${dsync.eyBase.retrieve-gm}")
    String eyBaseRetrieveGmApi;
    @Value("${dsync.eyBase.retrieve-chn}")
    String eyBaseRetrieveChnApi;
    @Value("${dsync.eyBase.retrieve-chn-slice}")
    String eyBaseRetrieveChnSliceApi;
    @Value("${dsync.eyBase.riverLengths-gm-hydl}")
    String eyBaseRetrieveGmHydlApi;
    @Value("${dsync.eyBase.reservoirs}")
    String eyBaseReservoirsApi;
    @Value("${dsync.eyBase.reservoirs-slice}")
    String eyBaseReservoirsSliceApi;
    @Value("${dsync.eyBase.channel-gm-hydl}")
    String eyBaseChannelGmHydlApi;
    @Value("${dsync.eyBase.reservoir-gm-hydl}")
    String eyBaseReservoirsGmApi;
    @Value("${dsync.eyBase.tsleep:2}")
    Integer tsleep;
    @Value("${dsync.eyBase.expireDay:15}")
    Integer eyBaseExpireDay;
    Integer pageSize = 15;

    public void dsyncEyBaseRetrieveGm() {
        log.info("dsyncEyBaseRetrieveGm...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = eyBaseRetrieveGmApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize).toJson();
            try {
                Response response = syncPost("eyBaseRetrieveGmApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveGmService.deleteByExpireDay(eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseRetrieveGm(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("河长拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseRetrieveGm(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseRetrieveGm...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        eyBaseRetrieveGmService.saveOrUpdateData(dto.toJavaObject(EyBaseRetrieveGmDto.class));
                    } catch (Exception e) {
                        log.error("河长,入库失败," + apiUrl + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("河长拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_河长, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseRetrieveRivers() {
        log.info("dsyncEyBaseRetrieveRivers...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = retrieveRiversApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize, true).toJson();
            try {
                Response response = syncPost("retrieveRiversApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    eyBaseRetrieveHydlService.deleteByExpireDay("rivers", eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseRetrieveRivers(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("河流拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseRetrieveRivers(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseRetrieveRivers...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        eyBaseRetrieveHydlService.saveOrUpdateData("rivers", dto.toJavaObject(EyBaseRetrieveHydlDto.class));
                    } catch (Exception e) {
                        log.error("河流基础数据,入库失败," + apiUrl + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("河流拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_河流, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseRetrieveReachPage() {
        log.info("dsyncEyBaseRetrieveReachPage...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = retrieveReachPageApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize, true).toJson();
            try {
                Response response = syncPost("retrieveReachPageApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveHydlService.deleteByExpireDay("reach", eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseRetrieveReachPage(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("河段拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseRetrieveReachPage(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseRetrieveReachPage...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        eyBaseRetrieveHydlService.saveOrUpdateData("reach", dto.toJavaObject(EyBaseRetrieveHydlDto.class));
                    } catch (Exception e) {
                        log.error("河段基础数据,入库失败," + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("河段拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_河段, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseReservoirsSlice() {
        log.info("dsyncEyBaseReservoirsSlice...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = eyBaseReservoirsSliceApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize, true).toJson();
            try {
                Response response = syncPost("eyBaseReservoirsSliceApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveReservoirsSliceService.deleteByExpireDay(eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseReservoirsSlice(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("库片拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseReservoirsSlice(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseReservoirsSlice...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        eyBaseRetrieveReservoirsSliceService.saveOrUpdateData(dto.toJavaObject(EyBaseRetrieveReservoirsSliceDto.class));
                    } catch (Exception e) {
                        log.error("库片基础数据,入库失败," + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("库片拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_库片, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseRetrieveRegion() {
        log.info("dsyncEyBaseRetrieveRegion...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = eyBaseRetrieveRegionApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize, true).toJson();
            try {
                Response response = syncPost("eyBaseRetrieveRegionApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveRegionService.deleteByExpireDay(eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseRetrieveRegion(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("行政区划拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseRetrieveRegion(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseRetrieveRegion...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        eyBaseRetrieveRegionService.saveOrUpdateData(dto.toJavaObject(EyBaseRetrieveRegionDto.class));
                    } catch (Exception e) {
                        log.error("行政区划,入库失败," + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("行政区划拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_行政区划, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseRetrieveChn() {
        log.info("dsyncEyBaseRetrieveChn...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = eyBaseRetrieveChnApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize, true).toJson();
            try {
                Response response = syncPost("eyBaseRetrieveChnApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveRegionService.deleteByExpireDay(eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseRetrieveChn(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("渠道拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseRetrieveChn(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseRetrieveChn...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        EyBaseRetrieveChnDto chnDto = dto.toJavaObject(EyBaseRetrieveChnDto.class);
                        chnDto.setSLong(dto.getDouble("_long"));
                        chnDto.setSLat(dto.getDouble("lat"));
                        eyBaseRetrieveChnService.saveOrUpdateData(chnDto);
                    } catch (Exception e) {
                        log.error("渠道数据,入库失败," + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("渠道拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_渠道, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseRetrieveChnSlice() {
        log.info("dsyncEyBaseRetrieveChnSlice...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = eyBaseRetrieveChnSliceApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize, true).toJson();
            try {
                Response response = syncPost("retrieveChnSliceApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveChnService.deleteByExpireDay(eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseRetrieveChnSlice(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("渠段拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseRetrieveChnSlice(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseRetrieveChnSlice...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        EyBaseRetrieveChnSliceDto chnDto = dto.toJavaObject(EyBaseRetrieveChnSliceDto.class);
                        chnDto.setSLong(dto.getDouble("_long"));
                        chnDto.setSLat(dto.getDouble("lat"));
                        eyBaseRetrieveChnSliceService.saveOrUpdateData(chnDto);
                    } catch (Exception e) {
                        log.error("渠段基础数据,入库失败," + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("渠段拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_渠段, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseRetrieveGmHydl() {
        log.info("dsyncEyBaseRetrieveGmHydl...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = eyBaseRetrieveGmHydlApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize).toJson();
            try {
                Response response = syncPost("eyBaseRetrieveGmHydlApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveGmHydlService.deleteByExpireDay(eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseRetrieveGmHydl(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("河段和河长关系拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseRetrieveGmHydl(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseRetrieveGmHydl...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        eyBaseRetrieveGmHydlService.saveOrUpdateData(dto.toJavaObject(EyBaseRetrieveGmHydlDto.class));
                    } catch (Exception e) {
                        log.error("河段和河长关系,入库失败," + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("河段和河长关系拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_河段VS河长, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseRetrieveChnGm() {
        log.info("dsyncEyBaseRetrieveChnGm...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = eyBaseChannelGmHydlApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize, true).toJson();
            try {
                Response response = syncPost("eyBaseRetrieveChnGmApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveChnGmService.deleteByExpireDay(eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseRetrieveChnGm(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("渠道_河长拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseRetrieveChnGm(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseRetrieveChnGm...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        eyBaseRetrieveChnGmService.saveOrUpdateData(dto.toJavaObject(EyBaseRetrieveGmHydlDto.class));
                    } catch (Exception e) {
                        log.error("渠道和河长关系,入库失败," + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("渠道和河长关系拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_渠道VS河长, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseReservoirs() {
        log.info("dsyncEyBaseReservoirs...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = eyBaseReservoirsApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize, true).toJson();
            try {
                Response response = syncPost("eyBaseReservoirsApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveReservoirsService.deleteByExpireDay(eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseReservoirs(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("水库拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseReservoirs(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseReservoirs...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        eyBaseRetrieveReservoirsService.saveOrUpdateData(dto.toJavaObject(EyBaseRetrieveReservoirsDto.class));
                    } catch (Exception e) {
                        log.error("水库,入库失败," + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("水库拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_水库, apiUrl, djson, response);
        }
        return pageDto;
    }

    public void dsyncEyBaseReservoirsGm() {
        log.info("dsyncEyBaseReservoirsGm...");
        Integer pageNum = 0;
        Integer pageCount = 1;
        EyBasePageResultDto pageDto = null;
        String apiUrl = eyBaseReservoirsGmApi;
        do {
            pageNum++;
            String djson = new EyBasePageDto(pageNum, pageSize).toJson();
            try {
                Response response = syncPost("eyBaseReservoirsGmApi" + pageNum, djson, apiUrl);
                if (response != null && response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 && pageNum == 1) {
                    eyBaseRetrieveReservoirsGmService.deleteByExpireDay(eyBaseExpireDay);
                }
                pageDto = dsyncEyBaseReservoirsGm(apiUrl, djson, response);
                if (pageDto != null) {
                    pageCount = pageDto.getPageCount(pageSize);
                }
                if (response != null && response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取) {
                    Thread.sleep(tsleep * 1000);
                }
            } catch (Exception e) {
                log.error("水库VS河长拉取", apiUrl);
                log.error("异常原因", e);
            }
        } while (pageDto != null && pageDto.isHasNextPage(pageNum, pageSize) || pageNum < pageCount);
    }

    public EyBasePageResultDto dsyncEyBaseReservoirsGm(String apiUrl, String djson, Response response) {
        log.info("dsyncEyBaseReservoirsGm...");
        EyBasePageResultDto pageDto = null;
        try {
            log.info("调用:" + apiUrl + " , 反馈:" + response.getCode());
            if (response.code != CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.data == null) {
                return pageDto;
            }
            JSONObject data = (JSONObject) response.data;
            pageDto = data.toJavaObject(EyBasePageResultDto.class);
            if (pageDto == null || pageDto.getDat() == null) {
                return pageDto;
            }
            List<JSONObject> list = pageDto.getDat();
            if (list != null) {
                list.forEach(dto -> {
                    try {
                        eyBaseRetrieveReservoirsGmService.saveOrUpdateData(dto.toJavaObject(EyBaseRetrieveGmHydlDto.class));
                    } catch (Exception e) {
                        log.error("水库VS河长,入库失败," + e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.error("水库VS河长拉取", apiUrl);
            log.error("异常原因", e);
        } finally {
            logDSyncAdd(CommonConstant.TASK_拉取_EY_水库VS河长, apiUrl, djson, response);
        }
        return pageDto;
    }
}
